// package org.xd.tn.main;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;


public class main {
    public static String getConnection(String path) {
        //String path = "";
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            if (conn.getResponseCode() == 200) {
                InputStream xml = conn.getInputStream();
                byte[] data = read(xml);
                //System.out.println(xml);
                return new String(data,Charset.forName("UTF-8"));
            } else {
                System.out.println("连接失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] read(InputStream xml) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = xml.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        xml.close();
        return outputStream.toByteArray();
    }

    public static String fanyi(String yw) {
        String en = URLEncoder.encode(yw, Charset.forName("UTF-8"));
        String api_key = "0120dc0822bf4e67";
        String json = getConnection("https://api.muxiaoguo.cn/api/Tn_tencent?api_key="+api_key+"&text=" + en);
        System.out.println(json);
        // JSONObject json1 = JSONObject.parseObject(json);
        // 非常好用 IDEA 搞 fastjson 还是QS
        // 我估计是 jdk 版本的问题
        // 下次换个 lib

        int start = json.indexOf("\"Translation\":\"")+15;
        int end = json.indexOf("\"", start);
        String translation = json.substring(start,end);

        System.out.println(translation);
        return translation;
    }

    public static void main(String[] args) {
        // 初始化
        Frame frame = new Frame("生草机（By XiaoDeng3386）");
        frame.setSize(350, 235);
        frame.setBackground(new Color(238, 238, 238));
        frame.setBackground(new Color(238, 238, 238));
        frame.setResizable(true);

        // 字体
        Font myFont = new Font("Microsoft Yahei", Font.PLAIN, 14);
        Font myFont1 = new Font("Microsoft Yahei", Font.PLAIN, 15);

        // 输入框
        TextArea text = new TextArea(8, 30);
        text.setText("你所热爱的，就是你的生活");
        text.setFont(myFont1);
        frame.add(text, BorderLayout.NORTH);

        // 提示框
        Label label1 = new Label();
        label1.setFont(myFont);
        label1.setText("生草次数：");
        frame.add(label1, BorderLayout.WEST);


        // 次数输入框
        TextField len = new TextField();
	len.setFont(myFont);
        len.setText("3");
        frame.add(len, BorderLayout.CENTER);

        // 开始按钮
        Button start = new Button("执行");
        start.setFont(myFont);
        frame.add(start, BorderLayout.EAST);

        // 监听器
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 窗口关闭
                System.exit(0);
            }
        });
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String txt = text.getText();
                int length = Integer.parseInt(len.getText());
                System.out.println(txt);
                for(int i = 0;i <= length;i++) {
                    txt = fanyi(txt);
                    txt = fanyi(txt);
                }
                System.out.println(txt);
                text.setText(txt);
                System.out.println("OK");
            }
        });

        // 显示窗口
        frame.setVisible(true);
    }
}
